package com.tony.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class CountDownLatchTest {
    public static int sum = 0;
    static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) {
        AtomicInteger ddd;
        System.out.println( countDownLatch.getCount());
        CountDownLatchTest add = new CountDownLatchTest();
        Thread t1 = add.new sum(10,20);
        Thread t2 =add.new sum(30,40);
        Thread t3 = add.new sum2(50,60);

        try {
            System.out.println("try:"+ countDownLatch.getCount());
            t1.start();
            System.out.println("t1:"+ countDownLatch.getCount());
            t2.start();
            System.out.println("t2:"+ countDownLatch.getCount());
            countDownLatch.await();
            System.out.println("await:" +countDownLatch.getCount());

            System.out.println(sum);
            t3.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class sum extends Thread{
        private int a;
        private int b;
        public sum(int a,int b){
            this.a =a;
            this.b =b;
        }
        @Override
        public void run() {
            System.out.println(a+":"+ countDownLatch.getCount());
            sum += (a+b);
            System.out.println(a+"+"+b+"="+sum);
            countDownLatch.countDown();
        }
    }
    class sum2 extends Thread{
        private int a;
        private int b;
        public sum2(int a,int b){
            this.a =a;
            this.b =b;
        }
        @Override
        public void run() {
            sum += (a+b);
            System.out.println(a+"+"+b+"="+sum);
        }
    }
}
